<?php
class orderController extends RPHP_Controller
{

    public function ajax()
    {
        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {
            $data = array('code' => 400, "msg" => "");

            //取消订单
            if ($_POST['act'] == 'cancel_order') {

                $data = $m->cancelOrder($_POST['order_id']);

            }
            //确认收货
            elseif ($_POST['act'] == "receive_order") {

                $data['code'] = $m->reveiveOrder($_POST['order_id']);
            }
            // 获取物流信息
            elseif ($_POST['act'] == "getexpress") {

                $data = $m->getCourier($_POST['shipping_id'], $_POST['shipping_name']);
            }
            // 申请退款
            elseif ($_POST['act'] == "order_refund") {

                $data = $m->saveOrderRefundStatus($_POST);
            }
            // 申请退货
            elseif ($_POST['act'] == "order_return_goods") {

                $data = $m->saveOrderReturnGoodsStatus($_POST);
            }
            // 填写快递信息
            elseif ($_POST['act'] == "apply_return") {

                $data = $m->saveApplyReturnGoodsStatus($_POST);
            }
            echo json_encode($data);
        }

    }

    //我的订单
    public function order()
    {
        $m = $this->model;

        $user = $m->checkAuth();
        if ($user) {

            $this->view->user = $user;
            $type             = isset($_GET['type']) ? intval($_GET['type']) : "";
            $is_pat           = isset($_GET['is_pat']) ? intval($_GET['is_pat']) : "";
            $order_id         = isset($_GET['order_id']) ? addslashes(trim($_GET['order_id'])) : "";

            if ($order_id != '') {
                $para['order_id'] = $order_id;
            }

            if ($type != "") {
                $para['type'] = $type;
            }

            if ($is_pat != "") {
                $para['is_pat'] = $is_pat;
            }
            if (!$_GET['page']) {
                $page = 1;
            } else {
                $page = intval($_GET['page']);
                if ($page < 1) {
                    $page = 1;
                }

            }

            $data = $m->getOrderList($para, $page);
            //获取页码
            if ($data['maxpage'] > 1) {
                $data['spage'] = $m->fun->spage($data['page'], $data['maxpage']);
            }

            $this->view->data = $data;

            if ($shop['info']['delivery_setting'] != "") {
                $this->view->delivery = unserialize($shop['info']['delivery_setting']);
            }

            $this->view->type   = $type;
            $this->view->is_pat = $is_pat;
            $this->view->order  = $m->getOrderCountByType($is_pat);
            $this->display();
        }
    }

    //我的订单
    public function order_ajax()
    {
        $m    = $this->model;
        $user = $m->checkAuth(false);
        if ($user) {
            $type     = !empty($_GET['type']) ? intval($_GET['type']) : "";
            $order_id = isset($_GET['order_id']) ? addslashes(trim($_GET['order_id'])) : "";
            $is_pat   = isset($_GET['is_pat']) ? intval($_GET['is_pat']) : "";

            if ($order_id != '') {
                $param['order_id'] = $order_id;
            }

            if ($type != "") {
                $param['type'] = $type;
            }
            if ($is_pat != "") {
                $param['is_pat'] = $is_pat;
            }

            if (!$_GET['page']) {
                $page = 1;
            } else {
                $page = intval($_GET['page']);
                if ($page < 1) {
                    $page = 1;
                }

            }

            $data = $m->getOrderList($param, $page,2);
            echo json_encode($data);

        }
    }
    //订单详情
    public function orderdetail()
    {
        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user['uid']) {
            $order_id = intval($_GET['order_id']);
            if ($order_id) {
                $this->view->data = $m->getOrderDetail($order_id);
                // PRINT_R($this->view->data);
                $this->display();

            }

        }
    }

    //申请退货
    public function apply_return_goods()
    {
        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user['uid']) {
            // $order_id = intval($_GET['order_id']);
            $rec_id = intval($_GET['rec_id']);

            if ($rec_id) {
                $data               = $m->getOrderGoodsDetail($rec_id);
                $exlist             = $m->getExpressCompany();
                $this->view->data   = $data;
                $this->view->exlist = $exlist;
                $this->display();

            }

        }

    }
    //支付页面
    public function pay()
    {
        $m    = $this->model;
        $user = $m->checkAuth();

        if ($user['uid'] > 0) {
            $goto     = true;
            $order_id = intval($_GET['order_id']);

            if ($user['app'] == 1) {
                $transaction_type = 2;
            } else {
                $transaction_type = 1;
            }

            if ($order_id > 0) {
                $data = $m->getSingleOrder($order_id, $user['uid'], $transaction_type);

                if ($data && $this->c['pay'][$transaction_type]) {

                    $para['channel_id']     = $this->c['pay'][$transaction_type]['channel_id'];
                    $para['out_trade_no']   = $data['order_id'];
                    $para['out_trade_type'] = "ORDER";
                    $para['total_fee']      = $data['pay_total_fee'];
                    $para['timestamp']      = $_SERVER['REQUEST_TIME'];
                    ksort($para);
                    foreach ($para as $key => $value) {
                        $tmp[] = $key . "=" . $value;
                    }
                    $stringSignTemp = implode("&", $tmp) . "&channer_key=" . $this->c['pay'][$transaction_type]['channer_key'];
                    $para['sign']   = strtoupper(md5($stringSignTemp));
                    $tmp[]          = "sign=" . $para['sign'];
                    $goto           = false;
                    $this->redirect($this->c['pay'][$transaction_type]['gate_url'] . "?" . implode("&", $tmp));

                }
            }
            if ($goto) {
                $this->redirect("/order/order");
            }

        }

    }

}
